#include <iostream>
using namespace std;
#include <fstream>
#include <string>

#include "Student.h"
//#include "Student.cpp"

#include "Teacher.h"
//#include "Teacher.cpp"

#include "Manager.h"
//#include "Manager.cpp"

#include "OrderFile.h"
//#include "OrderFile.cpp"

#include "globalFile.h"

//进入学生子菜单界面
void studentMenu(Identity*& student)
{
    while (true)
    {
        //调用学生子菜单
        student->openMenu();

        Student* stu = (Student*)student;

        int select = 0;
        cin >> select;  //接受用户选择

        if (select == 1)    //申请预约
        {
            stu->applyOrder();
        }
        else if (select == 2)   //查看自身预约
        {
            stu->showMyOrder();
        }
        else if (select == 3)   //查看所有预约
        {
            stu->showAllOrder();
        }
        else if (select == 4)   //取消预约
        {
            stu->cancelOrder();
        }
        else    //注销登录
        {
            cout << "注销成功！" << endl;
            return;
        }

    }
}

//进入教师子菜单界面
void TeacherMenu(Identity*& teacher)
{
    while (true)
    {
        //调用子菜单界面
        teacher->openMenu();

        Teacher* tea = (Teacher*)teacher;

        int select = 0;//接收用户选择

        cin >> select;

        if (select == 1)    //查看所有预约
        {
            tea->showAllOrder();
        }
        else if (select == 2)   //审核预约
        {
            tea->validOrder();
        }
        else
        {
            cout << "注销成功！" << endl;
            return;
        }
    }
}

//进入管理员子菜单界面
void managerMenu(Identity*& manager)
{
    while (true)
    {
        //调用管理员子菜单
        manager->openMenu();

        //将父类指针 转为子类指针，调用子类里其他接口
        Manager* man = (Manager*)manager;

        int select = 0;
        cout << "输入您的选择：" << endl;
        
        //接受用户选择
        cin >> select;

        if (select == 1)    //添加账号
        {
            cout << "添加账号" << endl;
            man->addPerson();
            
        }
        else if (select == 2)   //查看账号
        {
            cout << "查看账号" << endl;
            man->showPerson();
        }
        else if (select == 3)   //查看机房
        {
            cout << "查看机房" << endl;
            man->showComputer();
        }
        else if (select == 4)   //清空预约
        {
            man->cleanFile();
        }
        else
        {
            cout << "注销登录成功！" << endl;
            return;
        }
    }
}

//登陆功能  参数1 操作文件名    参数2 操作身份类型
void LoginIn(string fileName, int type)
{
    //父类指针，用于指向子类对象
    Identity* Person = NULL;

    //读文件
    ifstream ifs;
    ifs.open(fileName, ios::in);

    //判断文件是否存在
    if (!ifs.is_open())
    {
        cout << "文件不存在！" << endl;
        ifs.close();
        return;
    }

    //准备接受用户信息
    int id = 0;
    string name;
    string password;

    //判断身份
    if (type == 1)  //学生身份
    {
        cout << "请输入您的学号：" << endl;
        cin >> id;
    }
    else if (type == 2) //教师身份
    {
        cout << "请输入您的职工号：" << endl;
        cin >> id;
    }
    
    cout << "请输入用户名：" << endl;
    cin >> name;

    cout << "请输入密码：" << endl;
    cin >> password;

    if (type == 1)
    {
        //学生身份验证
        int fid;    //从文件中读取的id号
        string fname;   //从文件中获取的姓名
        string fpassword;   //从文件中获取的密码

        while (ifs >> fid && ifs >> fname && ifs >> fpassword)
        {
            //与用户输入的信息做对比
            if (fid == id && name == fname && fpassword == password)
            {
                cout << "学生验证登陆成功！" << endl;

                Person = new Student(id, name, password);

                //进入学生身份的子菜单
                studentMenu(Person);

                return;
            }
        }

    }
    else if (type == 2)
    {
        //教师身份验证
        int fid;    //从文件中读取的id号
        string fname;   //从文件中获取的姓名
        string fpassword;   //从文件中获取的密码

        while (ifs >> fid && ifs >> fname && ifs >> fpassword)
        {
            //与用户输入的信息做对比
            if (fid == id && name == fname && fpassword == password)
            {
                cout << "教师验证登陆成功！" << endl;

                Person = new Teacher(id, name, password);

                //进入教师身份的子菜单
                TeacherMenu(Person);

                return;
            }
        }
    }
    else if (type == 3)
    {
        //管理员身份验证
        string fname;   //从文件中获取的姓名
        string fpassword;   //从文件中获取的密码

        while (ifs >> fname && ifs >> fpassword)
        {
            //与用户输入的信息做对比
            if (name == fname && fpassword == password)
            {
                cout << "管理员验证登陆成功！" << endl;

                Person = new Manager(name, password);

                //进入管理员身份的子菜单
                managerMenu(Person);
                
                return;
            }
        }
    }

    cout << "验证登入失败！" << endl;
}

int main()
{
    int select = 0;     //用于接受用户的选择

    while (true)
    {
        cout << "=============欢迎来到机房预约系统===============" << endl;
        cout << endl << "请输入您的身份" << endl;
        cout << "\t\t------------------------" << endl;
        cout << "\t\t|                      |" << endl;
        cout << "\t\t|     1、学生代表      |" << endl;
        cout << "\t\t|                      |" << endl;
        cout << "\t\t|     2、老师          |" << endl;
        cout << "\t\t|                      |" << endl;
        cout << "\t\t|     3、管理员        |" << endl;
        cout << "\t\t|                      |" << endl;
        cout << "\t\t|     0、退出          |" << endl;
        cout << "\t\t|                      |" << endl;
        cout << "\t\t------------------------" << endl;

        cout << "请输入您的选择：";
        cin >> select;

        switch (select)
        {
        case 1:     //学生身份
            LoginIn(STUDENT_FILE, 1);
            break;

        case 2:     //老师身份
            LoginIn(TEACHER_FILE, 2);
            break;

        case 3:     //管理员身份
            LoginIn(ADMIN_FILE, 3);
            break;

        case 0:     //退出系统
            cout << "欢迎下次使用！" << endl;
            return 0;
            break;

        default:
            cout << "输入有误，请重新选择！" << endl;
            break;
        }
    }
    
    return 0;
}